#include "StdAfx.h"

#include "Point3D.h"
#include "Camera.h"
#include "Canvas.h"
#include "Image.h"

#include "Sphere.h"
#include "Plane.h"
#include "Triangle.h"
#include "Scene.h"

#include "Raytracer.h"
#include "Ray.h"

int main()
{
	// Create Canvas and Camera
	CCanvas canvas(1200,900);
	
	CCamera* camera = new CCamera(CPoint3D(-220,0,60.f),CPoint3D(-200,0,60.f), 40.f, 30.f);	
	
	// Create Scene
	CLight* l1 = new CLight(CPoint3D(-30,0,200));
	CLight* l2 = new CLight(CPoint3D(-30,30,10));

	CMaterial m1;
	m1.SetDiffuse(1.f);
	m1.SetReflection(0.f);
	m1.SetSpecular(0.f,1);

	CMaterial m2;
	m2.SetDiffuse(1.0f);
	m2.SetReflection(0.25f);
	m2.SetSpecular(0.2f,20);

	CMaterial m3;
	m3.SetDiffuse(1.f);
	m3.SetReflection(1.f);
	m3.SetSpecular(0.5f,20);

	CSphere* s1 = new CSphere(CPoint3D(0,0,70),25.f);
	s1->SetColor(1.0f,0.0f,0.0f);
	s1->SetMaterial(m3);
	CSphere* s2 = new CSphere(CPoint3D(-50,0,60),5.f);
	s2->SetColor(0.0f,1.0f,0.0f);
	s2->SetMaterial(m3);
	CPlane* p1 = new CPlane(CPoint3D(0,0,0),CPoint3D(0,0,1));
	p1->SetColor(0.0f,0.0f,1.0f);
	p1->SetMaterial(m2);
	CPlane* p2 = new CPlane(CPoint3D(130,0,0),CPoint3D(-1,0,0));
	p2->SetColor(1.0f,1.0f,0.0f);
	p2->SetMaterial(m2);
	//CTriangle* t1 = new CTriangle(CPoint3D(0,0,20),CPoint3D(0,0,60),CPoint3D(0,40,40));
	CTriangle* t1 = new CTriangle(CPoint3D(0,-50,0),CPoint3D(0,-50,100),CPoint3D(0,50,0));
	t1->SetColor(1.f,0.f,0.f);
	t1->SetMaterial(m3);

	CTriangle* t2 = new CTriangle(CPoint3D(0,-50,100),CPoint3D(0,50,100),CPoint3D(0,50,0));
	t2->SetColor(1.f,0.f,0.f);
	t2->SetMaterial(m3);

	CScene scene;
	scene.SetCamera(camera);
	scene.AddLight(l1);
	scene.AddLight(l2);
	//scene.AddObject(s1);
	//scene.AddObject(s2);
	scene.AddObject(p1);
	scene.AddObject(p2);
	scene.AddObject(t1);
	scene.AddObject(t2);

	//// Raytrace
	//CRaytracer tracer(scene, canvas);
	//tracer.Trace();
	//// Write Image
	//CImage::SaveImage("Testje.bmp",canvas);

	int count = 50;
	for (int i=0;i<count;i++)
	{
		camera->SetEye(CPoint3D(120*cos((float)i/count*TWO_PI),120*sin((float)i/count*TWO_PI),60.f));
		camera->SetLookAt(CPoint3D(100*cos((float)i/count*TWO_PI),100*sin((float)i/count*TWO_PI),60.f));		
	//	p2->SetPoint(CPoint3D(130-i*3.5f,0,0));
	//	l1->SetCentre(CPoint3D(40*cos((float)i/count*TWO_PI),40*sin((float)i/count*TWO_PI),70));
	//	l2->SetCentre(CPoint3D(40*sin((float)i/count*TWO_PI),40*cos((float)i/count*TWO_PI),70));
		canvas.Reset();
		CRaytracer tracer(scene, canvas);
		tracer.Trace();
		// Write Image
		std::stringstream stream;
		stream << "Testje" << i << ".bmp";
		CImage::SaveImage(stream.str(),canvas);
		printf("Saving %d/%d\n",i+1,count);
	}
}